package com.qfedu.entity.course02;

import java.util.LinkedList;

/**
 *
 */
public class JosephusProblem {
    public static void main(String[] args) {
        lastRemaining(10, 3);
    }

    public static void lastRemaining(int n, int m) {
        LinkedQueue<Integer> q = new LinkedQueue<>();
        for (int i = 1; i <= n; i++) {
            q.enQueue(i);
        }
        int i = 1;
        while (q.head != q.tail) {
            int element = q.removeQueue();
            if (i < m) {
                q.enQueue(element);
                i++;
            } else {
                i = 1;
                System.out.println(element);
            }
        }
    }

    public static void lastRemaining2(int n, int m) {
        LinkedList<Integer> q = new LinkedList<Integer>();
        for (int i = 1; i <= n; i++) {
            q.add(i);
        }
        int i = 1;
        while (q.size() > 0) {
            int element = q.poll();
            if (i < m) {
                q.add(element);
                i++;
            } else {
                i = 1;
                System.out.println(element);
            }
        }
    }
}
